// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="EndpointBase.cs" company="Scrum for Team System">
//   None
// </copyright>
// <summary>
//   Defines the EndpointBase type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------

namespace ScrumForTeamSystem.EventService.WebService
{
    using System.IO;
    using System.Web.Services;
    using System.Xml;
    using System.Xml.Serialization;

    using Infrastructure.Interfaces;

    using ScrumForTeamSystem.EventService.Services;

    /// <summary>
    /// Base Class that contains common methods used by all the Endpoint Services.
    /// </summary>
    public class EndpointBase : WebService
    {
        /// <summary>
        /// Event Queue Service instance
        /// </summary>
        private IEventQueueService eventQueueService;

        /// <summary>
        /// Gets TFSEventQueueService.
        /// </summary>
        /// <value>
        /// The tfs event queue service.
        /// </value>
        protected IEventQueueService TfsEventQueueService
        {
            get
            {
                return
                        this.eventQueueService =
                        this.eventQueueService ?? DependencyResolver.GetImplementation<IEventQueueService>();
            }
        }

        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <typeparam name="T">The type to return</typeparam>
        /// <param name="serializedType">Type of the serialized.</param>
        /// <returns>Intance of the specified type</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Not relevant in this suituation")]
        protected static T CreateInstance<T>(string serializedType) where T : new()
        {
            T customType;
            var serializer = new XmlSerializer(typeof(T));
            using (var stream = new StringReader(serializedType))
            {
                XmlReader xmlReader = new XmlTextReader(stream);

                customType = (T)serializer.Deserialize(xmlReader);
            }

            return customType;
        }
    }
}